﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_Radio: ControlBase
    {
        private List<SelectorItem> _lst = new List<SelectorItem>(5);
        private string _radioValue = "";
        private string _radioName = "";
        private string _label = "";
        private string _buttomTips = "";
        private string _id = "";
        

        public PageForm_Radio(ViewContext viewContext) : base(viewContext)
        {
            this.Css("mobileui-form-field weui-cells weui-cells_radio");
        }

        public PageForm_Radio FirstItem(string title, string value)
        {
            _lst.Insert(0, new SelectorItem(title, value));

            return this;
        }

        public PageForm_Radio Options(IEnumerable<SelectorItem> opts)
        {
            _lst.AddRange(opts);

            return this;
        }

        /// <summary>
        /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="titleFunc"></param>
        /// <param name="valueFunc"></param>
        /// <returns></returns>
        public PageForm_Radio Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
            Func<T, string> valueFunc)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

            return this;
        }

        /// <summary>
        /// 单选框的选项,用于enum的显示
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public PageForm_Radio Options(Type enumType)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(enumType));

            return this;
        }

        /// <summary>
        /// 单选框的name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PageForm_Radio Name(string name)
        {
            _radioName = name;

            return this;
        }



        public PageForm_Radio Checked(string value)
        {
            _radioValue = value;

            return this;
        }

        public PageForm_Radio Checked(int value)
        {
            _radioValue = value.ToStringEx();

            return this;
        }

        /// <summary>
        /// 标签
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public PageForm_Radio Label(string label)
        {
            _label = label;

            return this;
        }

        public bool HasError
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_radioName) && Html.ViewData.ModelState.ContainsKey(_radioName))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string ErrorMessage
        {
            get
            {
                if (!HasError)
                {
                    return String.Empty;
                }

                var state = Html.ViewData.ModelState[_radioName];

                var error = state.Errors.Select(x => x.ErrorMessage).JoinToString(' ');

                //ModelMetadata metadata = ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);

                //var error = string.Join("&nbsp&nbsp",
                //    Html.GetUnobtrusiveValidationAttributes(_name, metadata)
                //        .Select(x => x.Key.ToString() + "=\"" + x.Value + "\""));

                return HttpUtility.HtmlEncode(error);
            }
        }

        public override MvcHtmlString Render()
        {
              /*
                <div class="weui-cells weui-cells_radio">
                  <label class="weui-cell weui-check__label" for="x12">
                    <div class="weui-cell__bd">
                      <p>cell standard</p>
                    </div>
                    <div class="weui-cell__ft">
                      <input type="radio" name="radio1" class="weui-check" id="x12" checked="checked">
                      <span class="weui-icon-checked"></span>
                    </div>
                  </label>
                  <a href="javascript:void(0);" class="weui-cell weui-cell_link">
                    <div class="weui-cell__bd">添加更多</div>
                  </a>
                </div>
             */

            if (!string.IsNullOrWhiteSpace(_label))
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title weui-cells_radio_title {HasError.If("weui-cell_warn")}\"><label>{_label}:<i class=\"weui-icon-warn\" data-error=\"{ErrorMessage}\" ></i></label></div>");
            }
            
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            if (string.IsNullOrWhiteSpace(_radioValue))
            {
                _radioValue = _lst.FirstOrDefault()?.Value;
            }

            foreach (var item in _lst)
            {
                var id = "rd" + base.WeUI.NextControlID();
                var isChecked = _radioValue==item.Value;

                ViewContext.Writer.WriteLine($"<label class=\"weui-cell weui-check__label\" for=\"{id}\">");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cell__bd\">{item.Title}</div>");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cell__ft\"><input type=\"radio\" class=\"weui-check\" value=\"{item.Value}\" {isChecked.If("checked=\"checked\"")} name=\"{_radioName}\" id=\"{id}\"><span class=\"weui-icon-checked\"></span></div>");

                ViewContext.Writer.WriteLine("</label>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end weui-cells_radio -->");

            return MvcHtmlString.Empty;
        }
    }
}